﻿using Inovout.Cloud.Applications.Models;
using Inovout.Cloud.Applications.Security;
using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.Models;
using Inovout.Cloud.Services;
using Inovout.Memberships.Models;
using Inovout.Organizations.Memberships.Models;
using Inovout.Organizations.Memberships.Services;
using Inovout.Organizations.Models;
using Inovout.Security;
using Inovout.Services;
using Inovout.Test;
using System;
using System.Threading;

namespace Inovout.Cloud.Client.Storage.Tests
{
    public abstract class StorageTest : BaseTest
    {
        public ApplicationUser CloudApplicationUser { get; private set; }
        public Project CloudProject { get; private set; }
        public StorageTest()
        {
            string org_username = "org"+DateTime.Now.Ticks.ToString();
            string org_name = "org"+DateTime.Now.Ticks.ToString();
            string app_key = "app"+DateTime.Now.Ticks.ToString();
            IOrganizationMembershipService organizationMembershipService = ComponentRegistry.Resolve<IOrganizationMembershipService>();
            OrganizationMembership organizationMembership = organizationMembershipService.FindByUserName(org_username);
            if (organizationMembership == null)
            {
                FormsUser ownerUser = new FormsUser
                {
                    Name = org_username,
                    Password = "123456"
                };
                organizationMembership = organizationMembershipService.Create(
                     new OrganizationUnit { Name = org_name },
                     ownerUser
                     ).Membership as OrganizationMembership;
            }
            IApplicationUserService applicationUserService = ComponentRegistry.Resolve<IApplicationUserService>();
            ApplicationUser CloudApplicationUser = applicationUserService.FindByName(app_key);
            if (CloudApplicationUser == null)
            {
                CloudApplicationUser = new ApplicationUser
                {
                    Name = app_key,
                    Application = new Application
                    {
                        Name = app_key,
                        Membership = organizationMembership
                    }
                };
                applicationUserService.Save(CloudApplicationUser);
            }
            //membershipUser = ComponentRegistry.Resolve<IMembershipUserService>().FindByClaimedIdentifier(membershipIdentifier.ToString());
            MembershipUser membersipUser = null;
            Thread.CurrentPrincipal = new ApplicationUserPrincipal(CloudApplicationUser, membersipUser);

            IProjectService projectService = ComponentRegistry.Resolve<IProjectService>();
            CloudProject = new Project { Name = "project" + DateTime.Now.Ticks.ToString() };
            projectService.Save(CloudProject);



        }

    }
}
